{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "显著水平0.050下H0为False\n",
      "置信水平0.950下：\n",
      "sigma^2的置信区间为(0.000008, 0.000044)\n",
      "mu1-mu2的置信区间为(-0.0195, -0.0085)\n",
      "mu1-mu3的置信区间为(-0.0255, -0.0145)\n",
      "mu2-mu3的置信区间为(-0.0115, -0.0005)\n"
     ]
    }
   ],
   "source": [
    "#例8-6\n",
    "import numpy as np\n",
    "from utility import sfeVarianceAnalysis\n",
    "alpha=0.05\n",
    "X=np.array([np.array([0.236, 0.238, 0.248, 0.245, 0.243]),\n",
    "            np.array([0.257, 0.253, 0.255, 0.254, 0.261]),\n",
    "            np.array([0.258, 0.264, 0.259, 0.267, 0.262])])\n",
    "sfeVarianceAnalysis(X, alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "显著水平0.050下H0为False\n",
      "置信水平0.950下：\n",
      "sigma^2的置信区间为(9.272962, 49.139508)\n",
      "mu1-mu2的置信区间为(6.7482, 18.4518)\n",
      "mu1-mu3的置信区间为(-7.6518, 4.0518)\n",
      "mu2-mu3的置信区间为(-20.2518, -8.5482)\n"
     ]
    }
   ],
   "source": [
    "#练习8-4\n",
    "import numpy as np\n",
    "from utility import sfeVarianceAnalysis\n",
    "X=np.array([np.array([40, 42, 48, 45, 38]),\n",
    "            np.array([26, 28, 34, 32, 30]),\n",
    "            np.array([39, 50, 40, 50, 43])])\n",
    "alpha=0.05\n",
    "sfeVarianceAnalysis(X, alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False False]\n"
     ]
    }
   ],
   "source": [
    "#例8-12\n",
    "import numpy as np\n",
    "from utility import dfeVarianceAnalysis\n",
    "alpha=0.05\n",
    "X=np.array([[[58.2, 52.6],[56.2, 41.2],[65.3, 60.8]],\n",
    "            [[49.1, 42.8],[54.1, 50.5],[51.6, 48.4]],\n",
    "            [[60.1, 58.3],[70.9, 73.2],[39.2, 40.7]],\n",
    "            [[75.8, 71.5],[58.2, 51.0],[48.7, 41.4]]])\n",
    "H0=dfeVarianceAnalysis(X, alpha)\n",
    "print(H0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False False]\n"
     ]
    }
   ],
   "source": [
    "#练习8-8\n",
    "from utility import dfeVarianceAnalysis\n",
    "import numpy as np\n",
    "alpha=0.05\n",
    "X=np.array([[[15, 15, 17],[19, 19, 16],[16, 18, 21]],\n",
    "            [[17, 17, 17],[15, 15, 15],[19, 22, 22]],\n",
    "            [[15, 17, 16],[18, 17, 16],[18, 18, 18]],\n",
    "            [[18, 20, 22],[18, 16, 17],[17, 17, 17]]])\n",
    "H0=dfeVarianceAnalysis(X, alpha)\n",
    "print(H0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False]\n"
     ]
    }
   ],
   "source": [
    "#例8-13\n",
    "import numpy as np\n",
    "from utility import dfeVarianceAnalysis1\n",
    "alpha=0.05\n",
    "X=np.array([[76, 67, 81, 56, 51],\n",
    "          [82, 69, 96, 59, 70],\n",
    "          [68, 59, 67, 54, 42],\n",
    "          [63, 56, 64, 58, 37]])\n",
    "H0=dfeVarianceAnalysis1(X, alpha)\n",
    "print(H0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True]\n"
     ]
    }
   ],
   "source": [
    "#练习8-9\n",
    "import numpy as np\n",
    "from utility import dfeVarianceAnalysis1\n",
    "alpha=0.05\n",
    "X=np.array([[1.63, 1.35, 1.27],\n",
    "           [1.34, 1.30, 1.22],\n",
    "           [1.19, 1.14, 1.27],\n",
    "           [1.30, 1.09, 1.32]])\n",
    "H0=dfeVarianceAnalysis1(X, alpha)\n",
    "print(H0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=1.867,b=188.988,s2=404.856\n",
      "(0.300,3.433)\n",
      "(81.977,295.999)\n",
      "(230.890,1857.367)\n",
      "H0:a=0 is False\n",
      "correlation coefficient is 0.6968\n"
     ]
    }
   ],
   "source": [
    "#例8-19，8-20，8-21\n",
    "import numpy as np\n",
    "from scipy.stats import linregress\n",
    "from utility import muBounds, sigma2Bounds\n",
    "alpha=0.05\n",
    "x=np.array([51, 53, 60, 64, 68, 70, 70, 72, 83, 84])\n",
    "y=np.array([283, 293, 290, 286, 288, 349, 340, 354, 324, 343])\n",
    "n=x.size\n",
    "x_bar=x.mean()\n",
    "lxx=((x-x_bar)**2).sum()\n",
    "res=linregress(x, y)\n",
    "a=res.slope\n",
    "b=res.intercept\n",
    "s2=(res.stderr**2)*lxx*(n-2)/n\n",
    "print('a=%.3f,b=%.3f,s2=%.3f'%(a,b,s2))\n",
    "d=res.stderr\n",
    "(l1, r1)=muBounds(a, d, 1-alpha, n-2)\n",
    "d=res.intercept_stderr\n",
    "(l2, r2)=muBounds(b, d, 1-alpha, n-2)\n",
    "d=n*s2\n",
    "(l3, r3)=sigma2Bounds(d, n-2, 1-alpha)\n",
    "print('(%.3f,%.3f)'%(l1, r1))\n",
    "print('(%.3f,%.3f)'%(l2, r2))\n",
    "print('(%.3f,%.3f)'%(l3, r3))\n",
    "print('H0:a=0 is %s'%(res.pvalue>=alpha))\n",
    "print('correlation coefficient is %.4f'%res.rvalue)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=0.483,b=-2.739,s2=0.722\n",
      "(0.459,0.507)\n",
      "(-6.306,0.827)\n",
      "(0.412,3.314)\n",
      "H0:a=0 is False\n"
     ]
    }
   ],
   "source": [
    "#练习8-13，8-14，8-15\n",
    "import numpy as np\n",
    "from scipy.stats import linregress\n",
    "from utility import muBounds, sigma2Bounds \n",
    "alpha=0.05\n",
    "x=np.array([100,110,120,130,140,150,160,170,180,190])\n",
    "y=np.array([45,51,54,61,66,70,74,78,85,89])\n",
    "n=x.size\n",
    "x_bar=x.mean()\n",
    "lxx=((x-x_bar)**2).sum()\n",
    "res=linregress(x, y)\n",
    "a=res.slope\n",
    "b=res.intercept\n",
    "s2=(res.stderr**2)*lxx*(n-2)/n\n",
    "print('a=%.3f,b=%.3f,s2=%.3f'%(a,b,s2))\n",
    "d=res.stderr\n",
    "(l1, r1)=muBounds(a, d, 1-alpha, n-2)\n",
    "d=res.intercept_stderr\n",
    "(l2, r2)=muBounds(b, d, 1-alpha, n-2)\n",
    "d=n*s2\n",
    "(l3, r3)=sigma2Bounds(d, n-2, 1-alpha)\n",
    "print('(%.3f,%.3f)'%(l1, r1))\n",
    "print('(%.3f,%.3f)'%(l2, r2))\n",
    "print('(%.3f,%.3f)'%(l3, r3))\n",
    "print('H0:a=0 is %s'%(res.pvalue>alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(263.342, 372.259)\n"
     ]
    }
   ],
   "source": [
    "#例8-24\n",
    "import numpy as np\n",
    "from scipy.stats import linregress\n",
    "from utility import muBounds\n",
    "alpha=0.05\n",
    "x0=69\n",
    "x=np.array([51, 53, 60, 64, 68, 70, 70, 72, 83, 84])\n",
    "y=np.array([283, 293, 290, 286, 288, 349, 340, 354, 324, 343])\n",
    "n=x.size\n",
    "x_bar=x.mean()\n",
    "lxx=((x-x_bar)**2).sum()\n",
    "res=linregress(x, y)\n",
    "a=res.slope\n",
    "b=res.intercept\n",
    "s=res.stderr*np.sqrt((n-2)*lxx/n)\n",
    "d=np.sqrt((n+1)/(n-2)*s**2+\n",
    "          ((x0-x_bar)*res.stderr)**2)\n",
    "(l,r)=muBounds(a*x0+b, d, 1-alpha, n-2)\n",
    "print('(%.3f, %.3f)'%(l,r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(65.967, 70.565)\n"
     ]
    }
   ],
   "source": [
    "#练习8-18\n",
    "import numpy as np\n",
    "from scipy.stats import linregress\n",
    "from utility import muBounds\n",
    "alpha=0.05\n",
    "x=np.array([100,110,120,130,140,150,160,170,180,190])\n",
    "y=np.array([45,51,54,61,66,70,74,78,85,89])\n",
    "x0=147\n",
    "n=x.size\n",
    "x_bar=x.mean()\n",
    "lxx=((x-x_bar)**2).sum()\n",
    "res=linregress(x, y)\n",
    "a=res.slope\n",
    "b=res.intercept\n",
    "s=res.stderr*np.sqrt((n-2)*lxx/n)\n",
    "d=np.sqrt((n+1)/(n-2)*s**2+\n",
    "          ((x0-x_bar)*res.stderr)**2)\n",
    "(l,r)=muBounds(a*x0+b, d, 1-alpha, n-2)\n",
    "print('(%.3f, %.3f)'%(l,r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Y in (59, 60)\n"
     ]
    }
   ],
   "source": [
    "#例8-25\n",
    "import numpy as np\n",
    "from scipy.stats import linregress, norm\n",
    "from utility import controlbi\n",
    "alpha=0.05\n",
    "x=np.array([51, 53, 60, 64, 68, 70, 70, 72, 83, 84])\n",
    "y=np.array([283, 293, 290, 286, 288, 349, 340, 354, 324, 343])\n",
    "n=x.size\n",
    "x_bar=x.mean()\n",
    "lxx=((x-x_bar)**2).sum()\n",
    "res=linregress(x, y)\n",
    "a=res.slope\n",
    "b=res.intercept\n",
    "s=res.stderr*np.sqrt((n-2)/n*lxx)\n",
    "y1=260\n",
    "y2=340\n",
    "print('Y in (%.0f, %.0f)'%controlbi(a, b, s, y1, y2, alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(143, +oo)\n"
     ]
    }
   ],
   "source": [
    "#练习8-20\n",
    "import numpy as np\n",
    "from scipy.stats import linregress, norm\n",
    "from utility import controlright\n",
    "alpha=0.05\n",
    "x=np.array([100,110,120,130,140,150,160,170,180,190])\n",
    "y=np.array([45,51,54,61,66,70,74,78,85,89])\n",
    "n=x.size\n",
    "x_bar=x.mean()\n",
    "lxx=((x-x_bar)**2).sum()\n",
    "res=linregress(x, y)\n",
    "a=res.slope\n",
    "b=res.intercept\n",
    "s=res.stderr*np.sqrt((n-2)/n*lxx)\n",
    "y1=65\n",
    "print('(%.0f, +oo)'%controlright(a, b, s, y1, alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
